package com.wenhui.schoolyardinfo.login.ui;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.widget.QMUILoadingView;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.base.BaseMvpActivity;
import com.wenhui.schoolyardinfo.customview.CircleImageView;
import com.wenhui.schoolyardinfo.login.presenter.LoginPresenterImpl;
import com.wenhui.schoolyardinfo.login.presenter.base.ILoginPresenter;
import com.wenhui.schoolyardinfo.login.view.IRegisterView;
import com.wenhui.schoolyardinfo.login.view.base.IBaseView;
import com.wenhui.schoolyardinfo.manager.LogManager;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/2 15:39
 * introduce : 立即注册Activity
 */

public class RegisterNowActivity extends BaseMvpActivity<IBaseView, LoginPresenterImpl> implements IRegisterView {

    private static final String TAG = "RegisterNowActivity";
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.imv_head_portrait)
    CircleImageView imvHeadPortrait;
    @BindView(R.id.edt_input_account)
    EditText edtInputAccount;
    @BindView(R.id.edt_verification_code)
    EditText edtVerificationCode;
    @BindView(R.id.tev_get_verification_code)
    TextView tevGetVerificationCode;
    @BindView(R.id.edt_input_password)
    EditText edtInputPassword;
    @BindView(R.id.tev_register_now)
    TextView tevRegisterNow;
    private QMUILoadingView loadView;

    private Timer timer;
    private TimerTask timerTask;
    private int countDown;
    private boolean isStopTimer;
    private String cellPhoneNumber;

    private Map<String, String> bodyParams;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_now);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {

        isStopTimer = false;
        bodyParams = new HashMap<>();
        presenter = initPresenter();
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);
        topbar.removeAllLeftViews();

        loadView = new QMUILoadingView(this);
        loadView.setVisibility(View.GONE);
        loadView.setSize(120);

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        addContentView(loadView, layoutParams);
    }

    private void initTimer() {
        LogManager.i(TAG, "initTimer");
        if (timer == null) {
            timer = new Timer();
        }

        if (timerTask == null) {
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (countDown == 1) {
                                LogManager.i(TAG, "countDown:" + countDown);
                                tevGetVerificationCode.setText("获取验证码");
                                tevGetVerificationCode.setBackgroundColor(getResources().getColor(R.color.colorBlueA));
                                tevGetVerificationCode.setTextColor(getResources().getColor(R.color.colorWhiteB));
                                tevGetVerificationCode.setEnabled(true);
                                isStopTimer = true;
                                stopTimer();
                            }

                            if (!isStopTimer) {
                                LogManager.i(TAG, "countDown******:" + countDown);
                                countDown--;
                                tevGetVerificationCode.setBackgroundColor(getResources().getColor(R.color.colorGrayB));
                                tevGetVerificationCode.setTextColor(getResources().getColor(R.color.colorGrayA));
                                tevGetVerificationCode.setText("重新获取（" + countDown + "s）");
                            }
                        }
                    });
                }
            };
        }

        if (timer != null && timerTask != null) {
            timer.schedule(timerTask, 1000, 1000);
        }
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }

        if (timerTask != null) {
            timerTask.cancel();
            timerTask = null;
        }
    }

    @Override
    public void showLoading() {
        loadView.setVisibility(View.VISIBLE);
        loadView.start();
    }

    @Override
    public void hideLoading() {
        loadView.stop();
        loadView.setVisibility(View.GONE);
    }

    @Override
    protected LoginPresenterImpl initPresenter() {
        return new LoginPresenterImpl(this, this);
    }

    @Override
    public void registerSuccess(String success) {
        startActivity(AfterRegisteredActivity.class);
    }

    @Override
    public void registerError(String error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopTimer();
    }

    @OnClick({R.id.layout_back, R.id.tev_get_verification_code, R.id.tev_register_now})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.tev_get_verification_code:
                isStopTimer = false;
                countDown = 60;
                tevGetVerificationCode.setText("重新获取（" + countDown + "s）");
                tevGetVerificationCode.setBackgroundColor(getResources().getColor(R.color.colorGrayB));
                tevGetVerificationCode.setTextColor(getResources().getColor(R.color.colorGrayA));
                tevGetVerificationCode.setEnabled(false);
                initTimer();
                break;
            case R.id.tev_register_now:

                String userName = edtInputAccount.getText().toString();
                String verificationCode = edtVerificationCode.getText().toString();
                String password = edtInputPassword.getText().toString();

                if (userName != null && !"".equals(userName)) {
                    if (verificationCode != null && !"".equals(verificationCode)) {
                        if (password != null && !"".equals(password)) {
                            bodyParams.put("userName", userName);
                            bodyParams.put("verificationCode", verificationCode);
                            bodyParams.put("password", password);
                            presenter.register(bodyParams);
                        } else {
                            showToast(getResources().getString(R.string.please_input_a_password));
                        }
                    } else {
                        showToast(getResources().getString(R.string.please_enter_the_verification_code));
                    }
                } else {
                    showToast(getResources().getString(R.string.please_enter_one_user_name));
                }
                break;
        }
    }
}
